#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys

sys.path.append('..')
from common_utils import CBBCommonUtils


def get_plugin_info():
    plugin_info = {
        "name": "centos_anti_malware_001 Check_whether_chkrootkit_is_installed",
        "plugin_id": "centos_anti_malware_001",
        "plugin_type": "Anti-Malware",
        "info": "Check whether chkrootkit is installed",
        "level": "C",
        "module": "Safety reinforcement",
        "author": "yanlh",
        "keyword": "Safety reinforcement",
        "configable": "false"
    }
    return plugin_info


logger = None
cur_user = None
cur_module = None
cur_task = None


def set_plugin_logger(setter, user, module, task, *args, **kwargs):
    global logger, cur_user, cur_module, cur_task
    logger = setter
    cur_user = user
    cur_module = module
    cur_task = task


"""
存在四种按照方式：
1. rpm包安装，采用rpm -qa查看
2. deb包安装，采用dpkg -l查看
3. yum安装，采用yum list installed查看
4. 采用源码编译安装的，采用 find / -name 查看文件是否存在：这种方式不可靠，因此没有采用。
采用上面三种方式进行验证， 如果均不存在，则返回没有安装。
"""

cmd_list = ['rpm -qa', 'dpkg -l', 'yum list installed']


# 扫描函数
def scan(ip, sys_user, sys_pwd, flag=0):
    des_list = []
    error_count = 0
    for judge in cmd_list:
        cmd = judge + ' |grep chkrootkit'
        stderr, stdout = CBBCommonUtils.cbb_run_cmd(ip, cmd, username=sys_user, passwd=sys_pwd)
        if not stdout or stderr:
            continue
        des_list.append('Scan Complete.')
        logger.debug_info(cur_user, cur_module, cur_task + '_Scan', '', 'Scan Complete.')
        break
    if not des_list:
        error_count += 1
        des_list.append('chkrootkit is not installed')
        logger.debug_warning(cur_user, cur_module, cur_task + '_Scan', '', 'chkrootkit is not installed')
    return des_list, error_count


# 回滚函数
def rollback(ip, sys_user, sys_pwd):
    return ['Rollback Complete.'], 0


def check(ip, *args, **kwargs):
    sys_user = kwargs.get("system_user")
    sys_pwd = kwargs.get("system_pwd")
    comm = kwargs.get("command")
    try:
        des_list = []
        logger.debug_info(cur_user, cur_module, cur_task + '_Check', ip, 'centos_anti_malware_001 begin.')
        if comm == 1 or comm == 2:
            des_scan, error_scan = scan(ip, sys_user, sys_pwd)
            des_list.extend(des_scan)
            step_error = int(error_scan)
        elif comm == 3:
            des_rollback, error_rollback = rollback(ip, sys_user, sys_pwd)
            des_list.extend(des_rollback)
            step_error = int(error_rollback)
        else:
            return {"code": 3, "count": 0, "des": ['command must be 1/2/3']}
        if step_error == 0:
            code = 0
        elif step_error <= -1:
            code = 2
        else:
            code = 1
        logger.debug_info(cur_user, cur_module, cur_task + "_Check", ip, 'centos_anti_malware_001 end.')
        result = {"code": code, "count": step_error, "des": des_list}
        return result
    except Exception as er:
        code = 1
        logger.debug_error(cur_user, cur_module, cur_task + "_Check", ip, str(er))
        result = {"code": code, "count": 0, "des": ["ERROR:", str(er)]}
        return result

# check(ip="100.2.13.241", system_user="root", system_pwd="1qaz!QAZ", command=0, flag=0)
